import Cookies from "js-cookie";

const TokenKey = "access_token";
const IdTokenKey = "id_token";
const TenantIdKey = "__tenantId";

export function getToken() {
  return Cookies.get(TokenKey);
}

export function setToken(token) {
  return Cookies.set(TokenKey, token);
}

export function removeToken() {
  return Cookies.remove(TokenKey);
}

export function setIdToken(token) {
  return Cookies.set(IdTokenKey, token);
}

export function getIdToken() {
  return Cookies.get(IdTokenKey);
}

export function removeIdToken() {
  return Cookies.remove(IdTokenKey);
}

export function getTenantId() {
  return Cookies.get(TenantIdKey);
}

export function removeTenantId() {
  return Cookies.remove(TenantIdKey);
}

export function navToAuthor() {
  let authorUrl =
    process.env.VUE_APP_AuthServer +
    "/connect/authorize?client_id=" +
    process.env.VUE_APP_ClientId +
    "&redirect_uri=" +
    encodeURI(process.env.VUE_APP_URL + "/oidc") +
    "&response_type=id_token token&scope=openid roles profile AuthorCenter Finance Business&state=111&nonce=1234";
  window.location.href = authorUrl;
}

export function navToAuthorLogout() {
  let idToken = getIdToken();
  let logoutUrl =
    process.env.VUE_APP_AuthServer +
    "/connect/logout?id_token_hint=" +
    idToken +
    "&post_logout_redirect_uri=" +
    process.env.VUE_APP_URL +
    "/oidc_signout";
  window.location.href = logoutUrl;
}

export function navToLinkLogin(
  navToUrl,
  sourceLinkUserId,
  sourceLinkTenantId,
  targetLinkUserId,
  targetLinkTenantId,
  sourceLinkToken
) {
  let returnUrl = encodeURI(
    process.env.VUE_APP_Enterprise + "/oidc_link?to=" + navToUrl
  );

  let linkLoginUrl =
    process.env.VUE_APP_AuthServer +
    "/Account/Linklogin?ReturnUrl=" +
    returnUrl +
    "&SourceLinkUserId=" +
    sourceLinkUserId +
    "&SourceLinkTenantId=" +
    sourceLinkTenantId +
    "&TargetLinkUserId=" +
    targetLinkUserId +
    "&TargetLinkTenantId=" +
    targetLinkTenantId +
    "&SourceLinkToken=" +
    sourceLinkToken;
  window.location.href = linkLoginUrl;
}

